package com.hdsx.jxcsxm.log.util;

import com.hdsx.jxcsxm.log.bean.SysLog;
import com.hdsx.jxcsxm.log.type.BusinessType;
import com.opensymphony.xwork2.ActionInvocation;
import org.apache.struts2.dispatcher.HttpParameters;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 占贤银
 * time 2023/8/21 0:33
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
public final class CommonUtils {

    public static SysLog getCommonSyslog_insert(ActionInvocation invocation,
                                         HttpServletRequest request,
                                         BusinessType businessType) {
        SysLog sysLog = new SysLog();
        final HttpParameters parameters = invocation.getInvocationContext().getParameters();
        String opt_url = request.getRequestURI();
        String opt_ip = request.getRemoteAddr();
        String truename = request.getSession().getAttribute("truename").toString();
        String requestMethod = request.getMethod();
        String gydwbm = parameters.get("gydwdm").toString();
        String title = parameters.get("title").toString();
        sysLog.setTitle(title);
        sysLog.setBusiness_type(businessType.getState());
        sysLog.setOpt_type(businessType.getMsg());
        sysLog.setOpt_url(opt_url);
        sysLog.setOpt_ip(opt_ip);
        sysLog.setMethod(invocation.getInvocationContext().getName());
        sysLog.setRequest_method(requestMethod);
        sysLog.setOpt_param(parameters.toString());
        sysLog.setOpt_name(truename);
        sysLog.setOpt_dept(gydwbm);
        return sysLog;
    }

    public static SysLog getCommonSyslog(ActionInvocation invocation,
                                  HttpServletRequest request,
                                  BusinessType businessType) {
        SysLog sysLog = new SysLog();
        final HttpParameters parameters = invocation.getInvocationContext().getParameters();
        String opt_url = request.getRequestURI();
        String opt_ip = request.getRemoteAddr();
        String truename = request.getSession().getAttribute("truename").toString();
        String requestMethod = request.getMethod();
        String jsxz = parameters.get("xmjbxx.jsxz").toString();
        String gydwbm = parameters.get("xmjbxx.gydwdm").toString();
        StringBuffer sb = new StringBuffer();
        sb.append(businessType.getMsg());
        sb.append("->");
        if (gydwbm.startsWith("1")) {
            sb.append("农村公路");
        } else {
            sb.append("国省道");
        }
        sb.append("->");
        sb.append(jsxz);
        sysLog.setTitle(sb.toString());
        sysLog.setOpt_type("查询");
        sysLog.setBusiness_type(businessType.getState());
        sysLog.setOpt_url(opt_url);
        sysLog.setOpt_ip(opt_ip);
        sysLog.setMethod(invocation.getInvocationContext().getName());
        sysLog.setRequest_method(requestMethod);
        sysLog.setOpt_param(parameters.toString());
        sysLog.setOpt_name(truename);
        sysLog.setOpt_dept(gydwbm);
        return sysLog;
    }
}
